package io.finer.erp.jeecg.bas.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Dict;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.finer.erp.jeecg.bas.entity.ZzDevice;
import io.finer.erp.jeecg.bas.entity.ZzDeviceType;
import io.finer.erp.jeecg.bas.service.IZzDeviceService;
import io.finer.erp.jeecg.bas.service.IZzDeviceTypeService;
import io.finer.erp.jeecg.bas.service.IZzProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

 /**
 * @Description: 设备列表
 * @Author: jeecg-boot
 * @Date:   2020-09-01
 * @Version: V1.0
 */
@Api(tags="设备列表")
@RestController
@RequestMapping("/bas/zzDevice")
@Slf4j
public class ZzDeviceController extends JeecgController<ZzDevice, IZzDeviceService> {
	@Autowired
	private IZzDeviceService zzDeviceService;
	
	@Autowired
	private IZzProcessService zzProcessService;
	
	@Autowired
	private IZzDeviceTypeService deviceTypeService;
	
	/**
	 * 分页列表查询
	 *
	 * @param zzDevice
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "设备列表-分页列表查询")
	@ApiOperation(value="设备列表-分页列表查询", notes="设备列表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ZzDevice zzDevice,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ZzDevice> queryWrapper = QueryGenerator.initQueryWrapper(zzDevice, req.getParameterMap());
		Page<ZzDevice> page = new Page<ZzDevice>(pageNo, pageSize);
		IPage<ZzDevice> pageList = zzDeviceService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	@AutoLog(value = "查询设备字典")
	@ApiOperation(value="查询设备字典", notes="查询设备字典")
	@GetMapping(value = "/listDict")
	public Result<?> queryPageListByDict(ZzDevice zzDevice,HttpServletRequest req) {
		System.out.println("获取到的设备类型==="+zzDevice.getDeviceType());
		Map<String,Object> columnMap=new HashMap<String,Object>();
		List<Dict> listDict=new ArrayList<Dict>();
		columnMap.put("device_type", zzDevice.getDeviceType());
		Collection<ZzDevice> list=zzDeviceService.listByMap(columnMap);
		if(null!=list&&list.size()>0) {
			Iterator<ZzDevice> iterator=list.iterator();
			while(iterator.hasNext()) {
				ZzDevice zzDev=iterator.next();
				Dict dict=new Dict();
				dict.setValue(zzDev.getId());
				dict.setText(zzDev.getDeviceName());
				listDict.add(dict);
			}
		}
		return Result.ok(listDict);
	}
	
	/**
	 *   添加
	 *
	 * @param zzDevice
	 * @return
	 */
	@AutoLog(value = "设备列表-添加")
	@ApiOperation(value="设备列表-添加", notes="设备列表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ZzDevice zzDevice) {
		zzDeviceService.save(zzDevice);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param zzDevice
	 * @return
	 */
	@AutoLog(value = "设备列表-编辑")
	@ApiOperation(value="设备列表-编辑", notes="设备列表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ZzDevice zzDevice) {
		zzDeviceService.updateById(zzDevice);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "设备列表-通过id删除")
	@ApiOperation(value="设备列表-通过id删除", notes="设备列表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		zzDeviceService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "设备列表-批量删除")
	@ApiOperation(value="设备列表-批量删除", notes="设备列表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.zzDeviceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "设备列表-通过id查询")
	@ApiOperation(value="设备列表-通过id查询", notes="设备列表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ZzDevice zzDevice = zzDeviceService.getById(id);
		if(zzDevice==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(zzDevice);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param zzDevice
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZzDevice zzDevice) {
        return super.exportXls(request, zzDevice, ZzDevice.class, "设备列表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZzDevice.class);
    }

    /**
     * @Description 生产单要提交到mes上时获取设备信息。
     * @param type 1：复合 2：分切 3 切张
     * @return
     * Create at: 2020-12-22 17:31:05
     * @author: duany
     * Revision:
     *    2020-12-22 17:31:05 - first revision by duany
     *        
     */
    @GetMapping("/getOrderDevice")
    public Result<?> getOrderDevice(@RequestParam("type")Integer type){
    	return this.zzDeviceService.getOrderDevice(type);
    }
    
    /**
     * @Description 查询设备类型
     * @return
     * Create at: 2021-01-20 09:41:14
     * @author: duany
     * Revision:
     *    2021-01-20 09:41:14 - first revision by duany
     *        
     */
    @GetMapping("/getDeviceType")
    public Result<?> getDeviceType(){
    	List<ZzDeviceType> list = this.deviceTypeService
    			.lambdaQuery().ne(ZzDeviceType::getDeviceTpyeCode, "DEVICE_005").list();
    	return Result.ok(list);
    }
}
